/**
 * 新增留言
 * @param {String} content 留言内容，最多200字
 * @param {String} thumbs 图片，最多9张
 * @param {String} name 发表留言姓名
 */
import BaseController from "./common/BaseController"
import { isEmpty, getCurrentTime } from "../utils/utils"

class AddLeaveController extends BaseController {
    constructor() {
        super()
        this.mainMethod = this.mainMethod.bind(this)
        this.state = {
            request: null,
            response: null,
            next: null,
        }
    }

    mainMethod (req, res, next) {
        this.state.request = req
        this.state.response = res
        this.state.next = next
        this.handleBusiness()
    }

    async handleBusiness () {
        const { response, request } = this.state
        const { content, thumbs, name } = request.body
        if (isEmpty(content)) {
            response.json({ code: -1, msg: "缺少参数content" })
            return
        }
        if (isEmpty(name)) {
            response.json({ code: -1, msg: "缺少参数name" })
            return
        }

        await global.query('insert into wd_leave_message set content = ?,thumbs = ?,name = ?,create_time = ?', response, [content, thumbs, name, getCurrentTime("ts")])
        response.json({
            code: 1,
            msg: "添加成功",
        })
    }
}

export default new AddLeaveController().mainMethod
